package org.sean.framework.redis;

import org.springframework.data.redis.connection.DefaultStringRedisConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Key Value 都可以为任意类型
 * <pre>
 * 不支持数值型数据,自增减操作, 如: {@link org.springframework.data.redis.core.ValueOperations#increment(Object)}
 * </pre>
 *
 * @param <K> K
 * @param <V> V
 * @author Sean.xie
 */
public class ObjectJsonRedisTemplate<K, V> extends BaseRedisTemplate<K, V> {
    /**
     * Constructs a new <code>StringLongRedisTemplate</code> instance. {@link #setConnectionFactory(RedisConnectionFactory)}
     * and {@link #afterPropertiesSet()} still need to be called.
     */
    private ObjectJsonRedisTemplate() {
        StringRedisSerializer stringSerializer = new StringRedisSerializer();
        ObjectRedisSerializer<Object> objectSerializer = new ObjectRedisSerializer<>();
        setKeySerializer(stringSerializer);
        setValueSerializer(objectSerializer);
        setHashKeySerializer(objectSerializer);
        setHashValueSerializer(objectSerializer);
    }

    /**
     * Constructs a new <code>StringLongRedisTemplate</code> instance ready to be used.
     *
     * @param connectionFactory connection factory for creating new connections
     */
    public ObjectJsonRedisTemplate(RedisConnectionFactory connectionFactory) {
        this();
        setConnectionFactory(connectionFactory);
        afterPropertiesSet();
    }

    @Override
    protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
        return new DefaultStringRedisConnection(connection);
    }
}
